<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default.html(bodyClass='pear-container')}">
<head>
    <title>机构管理</title>
</head>
<body>
<section layout:fragment="wrapper">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div id="tb-tree-div" style="overflow: auto">
                        <ul id="tb-tree" class="dtree organizationTree" data-id=""></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" placeholder="" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">编码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="code" placeholder="" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-inline">
                                    <select name="status" class="layui-select">
                                        <option value="">全部</option>
                                        <option value="1">启用</option>
                                        <option value="2">禁用</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="tb-query">
                                    <i class="layui-icon layui-icon-search"></i>
                                    查询
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md">
                                    <i class="layui-icon layui-icon-refresh"></i>
                                    重置
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="tb-table" lay-filter="tb-table"></table>
                </div>
            </div>
        </div>
    </div>

    <script type="text/html" id="tb-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>

    <script type="text/html" id="tb-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="addChild">
            <i class="layui-icon layui-icon-add-1"></i>
        </button>
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </script>

    <script type="text/html" id="tb-type">
        {{#if (d.type == '0') { }}
        <span>集团</span>
        {{# }else if(d.type == '1'){ }}
        <span>公司</span>
        {{# }else if(d.type == '2'){ }}
        <span>部门</span>
        {{# }else if(d.type == '3'){ }}
        <span>科室</span>
        {{# }else if(d.type == '4'){ }}
        <span>小组</span>
        {{# } }}
    </script>

    <script type="text/html" id="tb-status">
        {{#if (d.status == '1') { }}
        <span class="layui-badge layui-bg-green">启用</span>
        {{# }else if(d.status == '2'){ }}
        <span class="layui-badge layui-bg-gray">禁用</span>
        {{# } }}
    </script>

    <script th:inline="javascript">
        layui.use(['table', 'form', 'jquery', 'dtree'], function () {
            let table = layui.table;
            let form = layui.form;
            let dtree = layui.dtree;
            let $ = layui.jquery;

            let treeIns = dtree.render({
                elem: "#tb-tree",
                initLevel: "2", //默认展开层级为1
                line: true, // 有线树
                ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
                icon: ["0", "2"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
                width: '100%',
                url: '[(@{/a/sys/office/data})]',
                method: 'post',
                dataFormat: "list",
                dataStyle: "layuiStyle",
                response: {message: "msg", statusCode: '20000'},
                scroll: "#tb-tree-div",
                menubar: true,
                menubarTips:{
                    group:["moveDown", "moveUp", "refresh", {
                        menubarId: "addNode",  // 按钮的唯一ID
                        icon: "dtree-icon-roundadd", // 从dtreefont库中选取
                        title: "新增",  // 按钮的提示标题
                        handler: function(node, $div){  //扩展按钮的回调函数
                            add();
                        }
                    }]
                },
                toolbar: true,
                toolbarWay: "fixed",
                toolbarShow: ["AddChildNode", "EditNode", "DelNode"],
                toolbarExt: [
                    {
                        toolbarId: "AddChildNode",
                        icon: "dtree-icon-fenzhijigou",
                        title: "新增子节点",
                        handler: function (node, $div) {
                            add(node.nodeId);
                        }
                    },
                    {
                        toolbarId: "EditNode",
                        icon: "dtree-icon-bianji",
                        title: "修改",
                        handler: function (node, $div) {
                            layer.open({
                                type: 2,
                                title: '修改',
                                shade: 0.1,
                                maxmin: true,
                                area: ['900px', '600px'],
                                content: '[(@{/a/sys/office/form})]?id=' + node.nodeId
                            });
                        }
                    },
                    {
                        toolbarId: "DelNode",
                        icon: "dtree-icon-delete1",
                        title: "删除",
                        handler: function (node, $div) {
                            removeData([node.nodeId]);
                        }
                    }
                ],
            });

            // 绑定节点点击事件
            dtree.on("node(tb-tree)", function (obj) {
                let where;
                if (!obj.param.leaf) {
                    where = {
                        parentId: obj.param.nodeId
                    }
                } else {
                    where = {
                        id: obj.param.nodeId
                    }
                }
                table.reload('tb-table', {
                    where: where || {},
                    page: {
                        curr: 1
                    }
                })
            });

            table.render({
                elem: '#tb-table',
                url: '[(@{/a/sys/office/page})]',
                method: 'post',
                response: {
                    statusCode: '20000',
                },
                page: true,
                height: 'full-150',
                cols: [
                    [
                        {type: 'checkbox'},
                        {title: '名称', field: 'title', align: 'center'},
                        {title: '编码', field: 'code', align: 'center'},
                        {title: '类型', field: 'type', align: 'center', templet: '#tb-type', width: 80},
                        {title: '地址', field: 'address', align: 'center', hide: true},
                        {title: '邮政编码', field: 'zipCode', align: 'center', hide: true},
                        {title: '主负责人', field: 'primaryPerson', align: 'center', hide: true},
                        {title: '状态', field: 'status', align: 'center', templet: '#tb-status', width: 80},
                        {title: '操作', toolbar: '#tb-bar', align: 'center'}
                    ]
                ],
                skin: 'line',
                toolbar: '#tb-toolbar',
                defaultToolbar: [
                    {title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh'},
                    'filter', 'exports', 'print'
                ]
            });

            table.on('tool(tb-table)', function (obj) {
                if (obj.event === 'remove') {
                    remove(obj);
                } else if (obj.event === 'edit') {
                    edit(obj);
                } else if (obj.event === 'addChild') {
                    add(obj.data['id']);
                }
            });

            table.on('toolbar(tb-table)', function (obj) {
                if (obj.event === 'add') {
                    add();
                } else if (obj.event === 'refresh') {
                    refresh();
                } else if (obj.event === 'batchRemove') {
                    batchRemove(obj);
                }
            });

            form.on('submit(tb-query)', function (data) {
                table.reload('tb-table', {
                    where: data.field,
                    page: {
                        curr: 1
                    }
                })
                return false;
            });

            const add = function (parentId) {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/a/sys/office/form})]?parentId=' + (parentId || '')
                });
            }

            const edit = function (obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/a/sys/office/form})]?id=' + obj.data['id']
                });
            }

            const remove = function (obj) {
                removeData([obj.data['id']]);
            }

            const batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                removeData(ids);
            }

            const removeData = function (array) {
                layer.confirm('确定要删除数据', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: '[(@{/a/sys/office/del})]',
                        data: {datas: array},
                        dataType: 'json',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code === '20000') {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    refresh();
                                    refreshTree();
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            const refresh = function (param) {
                table.reload('tb-table');
            }

            const refreshTree = function (param) {
                dtree.reload('tb-tree');
            }
        })
    </script>
</section>
</body>
</html>
